package com.dreamstations.changshads.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

import java.util.List;

/**
 * Created by 7YHong
 */

public class DSRvAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
    private static final String TAG = DSRvAdapter.class.getName();

    public static int VIEW_TYPE_HEADER = 1 << 3;
    public static int VIEW_TYPE_FOOTER = 1 << 4;
    public static int FOOTER_STATE_PENDING = 0;
    public static int FOOTER_STATE_REFRESHING = 1;

    protected Context mContext;
    protected List<T> mDatas;

    protected int dataOffset;
    protected int foot_state;

    protected RvSupport<T> item;
    private OnItemClickListener mOnItemClickListener;
    protected RvSupport header, footer;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public void setHeader(RvSupport header) {
        this.header = header;
        if (header != null) dataOffset = 1;
    }

    public void setFooter(RvSupport footer) {
        this.footer = footer;
    }

    public void removeFooter() {
        if (footer != null)
            notifyItemRemoved(getItemCount());
        this.footer = null;
    }

    public DSRvAdapter(Context context, List<T> datas, RvSupport<T> item) {
        mContext = context;
        mDatas = datas;
        this.item = item;
    }

    public DSRvAdapter(Context mContext, List<T> mDatas, RvSupport<T> item, OnItemClickListener mOnItemClickListener, RvSupport header, RvSupport footer) {
        this.mContext = mContext;
        this.mDatas = mDatas;
        this.item = item;
        this.mOnItemClickListener = mOnItemClickListener;
        this.header = header;
        if (header != null) dataOffset = 1;
        this.footer = footer;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0 && header != null) return VIEW_TYPE_HEADER;
        if (position == mDatas.size() + dataOffset) return VIEW_TYPE_FOOTER;
        return item.getItemViewType(mDatas.get(position - dataOffset));
    }

    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
        ViewHolder viewHolder = null;
        if (viewType == VIEW_TYPE_HEADER) {
            viewHolder = header.createHolder(mContext, parent, viewType);
        } else if (viewType == VIEW_TYPE_FOOTER) {
            viewHolder = footer.createHolder(mContext, parent, viewType);
        } else {
            viewHolder = item.createHolder(mContext, parent, viewType);
            setListener(parent, viewHolder, viewType);
        }
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.updatePosition(position);
        if (getItemViewType(position) == VIEW_TYPE_FOOTER) {
            footer.convert(holder, foot_state);
        } else if (getItemViewType(position) != VIEW_TYPE_HEADER) {
            item.convert(holder, mDatas.get(position - dataOffset));
        }
    }

    protected void setListener(final ViewGroup parent, final ViewHolder viewHolder, int viewType) {
        viewHolder.getConvertView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = viewHolder.getAdapterPosition() - dataOffset;
                    mOnItemClickListener.onItemClick(parent, v, mDatas.get(position), position);
                }
            }
        });


        viewHolder.getConvertView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClickListener != null) {
                    int position = viewHolder.getAdapterPosition() - dataOffset;
                    return mOnItemClickListener.onItemLongClick(parent, v, mDatas.get(position), position);
                }
                return false;
            }
        });
    }


    @Override
    public int getItemCount() {
        int i = 0;
        if (header != null) i++;
        if (footer != null) i++;
        i += mDatas.size();
//        L.i(TAG, "=>getItemCount:" + String.valueOf(i));
        return i;
    }

    public void setFooter_state(int state) {
        foot_state = state;
    }

    public void clear() {
        mDatas.clear();
        notifyDataSetChanged();
    }

    public void setItemDatas(List<T> datas) {
        mDatas.clear();
        mDatas.addAll(datas);
        notifyDataSetChanged();
    }

    public void addMoreItemData(List<T> datas) {
        int itemcount = getItemCount();
        mDatas.addAll(datas);
        notifyItemInserted(itemcount);
    }

    public void addMoreItemData(T data) {
        int itemcount = getItemCount();
        mDatas.add(data);
        notifyItemInserted(itemcount);
    }

    public interface RvSupport<T> {

        /**
         * 仅仅在需要支持多种布局的时候才需要进行设置
         *
         * @param data 当前元素
         * @return view类型
         */
        int getItemViewType(T data);

        ViewHolder createHolder(Context context, ViewGroup parent, int viewType);

        void convert(ViewHolder holder, T data);
    }
}
